package atm;

import lesson14.Student;

import java.sql.*;

/**
 * 用户的数据访问对象
 */
public class UserDao {
    /** 数据库连接字符串 */
    private String url = "jdbc:mysql://localhost:3306/j183?" +
            "userSSL=true&useUnicode=true&" +
            "characterEncoding=UTF8&serverTimezone=GMT";
    private String user = "root";
    private String pwd = "lovo";
    /** 数据库连接接口 */
    private Connection con;
    /** 执行SQL语句的对象 */
    private PreparedStatement statement;
    /** 结果集对象 */
    private ResultSet rs;
    /**
     * 获取数据库连接
     */
    public void getConnection(){
        con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 获取连接
            con = DriverManager.getConnection(url,user,pwd);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        System.out.println("数据库连接：" + con);
    }

    /**
     * 关闭所有连接
     */
    public void close(){
        // 关闭连接
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 根据账号和密码查询用户
     * @return
     */
    public User findByAccountAndPwd(String account,String pwd){
        User user = null;
        try{
            this.getConnection();
            statement = this.con.prepareStatement("select * from user where account=? and pwd=?");
            this.statement.setString(1,account);
            this.statement.setString(2,pwd);
            this.rs = this.statement.executeQuery();
            if(rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setAccount(rs.getString("account"));
                user.setPwd(rs.getString("pwd"));
                user.setBlance(rs.getDouble("blance"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            this.close();
        }
        return user;
    }

    /**
     * 根据账号查询用户
     * @param account
     * @return
     */
    public User findByAccount(String account){
        {
            User user = null;
            try{
                this.getConnection();
                statement = this.con.prepareStatement("select * from user where account=?");
                this.statement.setString(1,account);
                this.rs = this.statement.executeQuery();
                if(rs.next()){
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setAccount(rs.getString("account"));
                    user.setPwd(rs.getString("pwd"));
                    user.setBlance(rs.getDouble("blance"));
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally {
                this.close();
            }
            return user;
        }
    }
    public int update(User user){
        int i = 0;
        try{
            this.getConnection();
            this.statement = this.con.prepareStatement("update user set pwd=?,blance=? where id=?");
            this.statement.setString(1,user.getPwd());
            this.statement.setDouble(2,user.getBlance());
            this.statement.setInt(3,user.getId());
            i = this.statement.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            this.close();
        }
        return i;
    }
}
